package com.egao.common.module.pushInformation.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.entity.Position;
import com.egao.common.module.company.utils.PositionAddressUtil;
import com.egao.common.module.company.utils.PositionNameUtil;
import com.egao.common.module.pushInformation.entity.PushInformationPosition;
import com.egao.common.module.pushInformation.mapper.PushInformationPositionMapper;
import com.egao.common.module.pushInformation.service.PushInformationPositionService;
import com.egao.common.module.student.constants.StudentConstants;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.student.service.StudentSignService;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.Major;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.DictService;
import com.egao.common.module.system.service.MajorService;
import com.egao.common.module.system.service.YearService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lym
 * @date 2024/8/23 10:31
 */
@Service
public class PushInformationPositionServiceImpl extends ServiceImpl<PushInformationPositionMapper, PushInformationPosition> implements PushInformationPositionService {

    @Resource
    private YearService yearService;
    @Resource
    private StudentService studentService;
    @Resource
    private MajorService majorService;
    @Resource
    private StudentSignService studentSignService;
    @Resource
    private DictService dictService;
    @Resource
    private CollegeService collegeService;

    @Override
    public List<Student> selectStudent(String majorNames) {
        // 查询当前有效年份和上一年
        List<Integer> yearList = yearService.yearAndLastYear();
        if (CollectionUtil.isEmpty(yearList)) {
            return null;
        }

        // 查询专业代码
        if (StrUtil.isBlank(majorNames)) {
            return null;
        }
        String[] majorNameArray = majorNames.split(",");
        List<Major> majorList = majorService.list(new LambdaQueryWrapper<Major>().in(Major::getName, majorNameArray));
        if (CollectionUtil.isEmpty(majorList)) {
            return null;
        }
        List<String> majorCodeList = majorList.stream().map(Major::getCode).collect(Collectors.toList());

        // 查询求职学生
        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>()
                .in(Student::getYearId, yearList)
                .in(Student::getMajorCode, majorCodeList)
                .like(Student::getInten, "求职")
        );
        if (CollectionUtil.isEmpty(studentList)) {
            return null;
        }
        List<Integer> studentIdList = studentList.stream().map(Student::getId).collect(Collectors.toList());

        // 查询求职学生中已完成毕业去向的学生并且排除掉
        List<StudentSign> studentSignList = studentSignService.list(new LambdaQueryWrapper<StudentSign>()
                .select(StudentSign::getStudentId)
                .ne(StudentSign::getSignCategory, StudentSignConstants.SIGN_CATEGORY_DJY)
                .eq(StudentSign::getState, StudentSignConstants.STATE_VALID)
                .eq(StudentSign::getSignState, StudentSignConstants.SIGN_STATE_SCHOOL_PASS)
                .eq(StudentSign::getDispatchState, StudentSignConstants.DISPATCH_STATE_SCHOOL_PASS)
                .in(StudentSign::getStudentId, studentIdList)
        );
        if (!CollectionUtil.isEmpty(studentSignList)) {
            List<Integer> studentIds = studentSignList.stream().map(StudentSign::getStudentId).collect(Collectors.toList());
            List<Student> removeList = studentList.stream().filter(d -> studentIds.contains(d.getId())).collect(Collectors.toList());
            studentList.removeAll(removeList);
        }

        // 查询学生学院名称和专业名称
        List<College> collegeList = collegeService.list();
        for (Student student : studentList) {
            String collegeName = collegeList.stream().filter(d -> d.getCode().equals(student.getCollegeCode())).map(College::getName).findFirst().orElse(null);
            String majorName = majorList.stream().filter(d -> d.getCode().equals(student.getMajorCode())
                    && d.getCollegeCode().equals(student.getCollegeCode())).map(Major::getName).findFirst().orElse(null);
            student.setCollegeName(collegeName);
            student.setMajorName(majorName);
        }

        return studentList;
    }

    @Override
    public List<PushInformationPosition> computeStudentPositionMatching(List<Student> studentList, Position position, Company company) {
        // 单位性质、行业、规模
        String companyNature = dictService.redisLabel(DictConstants.COMPANY_NATURE, company.getNature());
        String companyIndustry = dictService.redisLabel(DictConstants.COMPANY_INDUSTRY, company.getIndustry());
        String companyScale = dictService.redisLabel(DictConstants.COMPANY_SCALE, company.getScale());

        // 最低分要求
        int min = 85;

        List<PushInformationPosition> list = new ArrayList<>();
        for (Student student : studentList) {
            // 学生缺少信息过滤掉
            if (StrUtil.isBlank(student.getIntenName()) || StrUtil.isBlank(student.getXl())) {
                continue;
            }

            // 初始分
            int score = 50;

            // 学历过滤
            String xl = "";
            if (StudentConstants.STUDENT_XL_BK.equals(student.getXl()) || StudentConstants.STUDENT_XL_BKRXW.equals(student.getXl())) {
                xl = "本科";
            }
            if (StudentConstants.STUDENT_XL_SS.equals(student.getXl())) {
                xl = "硕士";
            }
            if (StudentConstants.STUDENT_XL_BS.equals(student.getXl())) {
                xl = "博士";
            }
            if (!StrUtil.isBlank(position.getDegree()) && position.getDegree().contains(xl)) {
                score += 9;
            }

            // 意向职位过滤
            boolean positionNameBoolean = PositionNameUtil.matching(student.getIntenName(), position.getName());
            if (positionNameBoolean) {
                score += 8;
            }

            // 意向地区过滤
            boolean positionAddressBoolean = PositionAddressUtil.matching(student.getIntenAddress(), position.getAddress());
            if (positionAddressBoolean) {
                score += 9;
            }

            // 意向薪资过滤
            if (!StrUtil.isBlank(student.getSalary()) && student.getSalary().equals(position.getSalary())) {
                score += 9;
            }

            // 意向单位性质
            if (!StrUtil.isBlank(student.getIntenNature()) && student.getIntenNature().contains(companyNature)) {
                score += 5;
            }

            // 意向单位行业
            if (!StrUtil.isBlank(student.getIntenIndustry()) && student.getIntenIndustry().contains(companyIndustry)) {
                score += 5;
            }

            // 意向单位规模
            if (!StrUtil.isBlank(student.getIntenScale()) && student.getIntenScale().contains(companyScale)) {
                score += 5;
            }

            // 低于最低分不记录
            if (score < min) {
                continue;
            }

            // 记录数据
            PushInformationPosition pushInformationPosition = new PushInformationPosition();
            pushInformationPosition.setYear(student.getYearId());
            pushInformationPosition.setStudentId(student.getId());
            pushInformationPosition.setXh(student.getXh());
            pushInformationPosition.setXm(student.getXm());
            pushInformationPosition.setCollegeName(student.getCollegeName());
            pushInformationPosition.setMajorName(student.getMajorName());
            pushInformationPosition.setPositionId(position.getId());
            pushInformationPosition.setPositionName(position.getName());
            pushInformationPosition.setMatching(score);
            list.add(pushInformationPosition);
        }

        return list;
    }
}
